home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C06 / Constructor1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  949 b   |  47 lines

  1. //: C06:Constructor1.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Constructors & destructors
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. class Tree {
  11.   int height;
  12. public:
  13.   Tree(int initialHeight);  // Constructor
  14.   ~Tree();  // Destructor
  15.   void grow(int years);
  16.   void printsize();
  17. };
  18.  
  19. Tree::Tree(int initialHeight) {
  20.   height = initialHeight;
  21. }
  22.  
  23. Tree::~Tree() {
  24.   cout << "inside Tree destructor" << endl;
  25.   printsize();
  26. }
  27.  
  28. void Tree::grow(int years) {
  29.   height += years;
  30. }
  31.  
  32. void Tree::printsize() {
  33.   cout << "Tree height is " << height << endl;
  34. }
  35.  
  36. int main() {
  37.   cout << "before opening brace" << endl;
  38.   {
  39.     Tree t(12);
  40.     cout << "after Tree creation" << endl;
  41.     t.printsize();
  42.     t.grow(4);
  43.     cout << "before closing brace" << endl;
  44.   }
  45.   cout << "after closing brace" << endl;
  46. } ///:~
  47.